package com.ssl.sslliving.commodity.service.impl;

import com.ssl.sslliving.commodity.vo.Catalog2Vo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssl.common.utils.PageUtils;
import com.ssl.common.utils.Query;

import com.ssl.sslliving.commodity.dao.CategoryDao;
import com.ssl.sslliving.commodity.entity.CategoryEntity;
import com.ssl.sslliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public List<CategoryEntity> listTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> categoryTrees = entities.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId() == 0;
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, entities));
            return categoryEntity;
        }).sorted((categoryEntity1, categoryEntity2) -> {
            return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) -
                    (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
        }).collect(Collectors.toList());
        return categoryTrees;
    }

    private List<CategoryEntity> getChildrenCategories(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().equals(root.getId());
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {
        ArrayList<Long> cascadedCategoryId = new ArrayList<>();
        getParentCategoryId(categoryId, cascadedCategoryId);
        Collections.reverse(cascadedCategoryId);
        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }

    @Override
    public List<CategoryEntity> getLevel1Categories() {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<CategoryEntity> categoryEntities = this.baseMapper.selectList(queryWrapper);
        return categoryEntities;
    }

    private List<CategoryEntity> getParent_cid(List<CategoryEntity> selectList, Long parentCId) {
        List<CategoryEntity> categoryEntities = selectList.stream().filter(item -> {
            return item.getParentId().equals(parentCId);
        }).collect(Collectors.toList());
        return categoryEntities;
    }

    @Override
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        List<CategoryEntity> selectList = this.baseMapper.selectList(null);
        List<CategoryEntity> level1Categories = getParent_cid(selectList, 0L);
        Map<String, List<Catalog2Vo>> categoryMap =
                level1Categories.stream().collect(Collectors.toMap(k -> {
                    return k.getId().toString();
                }, v -> {
                    List<Catalog2Vo> catalog2Vos = new ArrayList<>();
                    List<CategoryEntity> level2Categories = getParent_cid(selectList, v.getId());
                    if (level2Categories != null && level2Categories.size() > 0) {
                        catalog2Vos = level2Categories.stream().map(l2 -> {
                            Catalog2Vo catalog2Vo = new Catalog2Vo(v.getId().toString(), null, l2.getId().toString(), l2.getName());
                            List<CategoryEntity> level3Categories = getParent_cid(selectList, l2.getId());
                            if (level3Categories != null && level3Categories.size() > 0) {
                                List<Catalog2Vo.Category3Vo> category3Vos = level3Categories.stream().map(l3 -> {
                                    Catalog2Vo.Category3Vo category3Vo =
                                            new Catalog2Vo.Category3Vo(l2.getId().toString(), l3.getId().toString(), l3.getName());
                                    return category3Vo;
                                }).collect(Collectors.toList());
                                catalog2Vo.setCatalog3List(category3Vos);
                            }
                            return catalog2Vo;
                        }).collect(Collectors.toList());
                    }
                    return catalog2Vos;
                }));
        return categoryMap;
    }

    private void getParentCategoryId(Long categoryId, List<Long> cascadedCategoryId) {
        cascadedCategoryId.add(categoryId);
        CategoryEntity categoryEntity = this.getById(categoryId);
        if (!categoryEntity.getParentId().equals(0L)) {
            getParentCategoryId(categoryEntity.getParentId(), cascadedCategoryId);
        }
    }
}